﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NHibernate;
using WebSite.Core;
using WebSite.Data.NH.Infrastructure;
using WebSite.Model;
using NHibernate.Linq;

namespace WebSite.Data.NH.Repositories
{
    public interface IContentRepository : IRepository<ContentInfo>
    {
        /// <summary>
        /// 通过Id获取
        /// </summary>
        /// <param name="ContentId">Id</param>
        /// <returns>ContentInfo</returns>
        ContentInfo GetContent(int ContentId);

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="Content">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool AddContent(ContentInfo Content);


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="ContentId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool DeleteContent(int ContentId);


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="Content">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool UpdateContent(ContentInfo Content);

        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        IList<ContentInfo> LoadAll(Expression<Func<ContentInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<ContentInfo>[] orderByExpressions);


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        int LoadCount(Expression<Func<ContentInfo, bool>> where);
    }

    public class ContentRepository : NhRepositoryBase<ContentInfo>, IContentRepository
    {
        private readonly IUnitOfWork uow;

        public ContentRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }




        public IList<ContentInfo> LoadAll(Expression<Func<ContentInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<ContentInfo>[] orderByExpressions)
        {
            var query = GetMany(where, pageIndex, pageSize, orderByExpressions);
            query = query.Fetch(e => e.ContentDetail);
            return query.ToList();
        }

        public int LoadCount(Expression<Func<ContentInfo, bool>> where)
        {
            var query = GetMany(where);
            return query.Count();
        }

        public ContentInfo GetContent(int ContentId)
        {
            var content = GetById(ContentId);
            return content;
        }

        public bool AddContent(ContentInfo Content)
        {
            bool result = false;
            result = Add(Content);
            uow.Commit();
            return result;
        }

        public bool DeleteContent(int ContentId)
        {
            bool result = false;
            var content = GetById(ContentId);
            result = Delete(content);
            uow.Commit();
            return result;
        }

        public bool UpdateContent(ContentInfo Content)
        {
            bool result = false;
            result = Update(Content);
            uow.Commit();
            return result;
        }
    }

}
